############### CMAKE 参数 #####################

set(CMAKE_VERBOSE_MAKEFILE OFF)
set(CMAKE_COLOR_MAKEFILE ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
#set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)

# 对依赖检查目录进行了过滤，如果这个选项被打开，
# 依赖检查目录列表就会忽略外部的第三方库头文件，这样就不会导致产生一个超大的depend.make文件了
# https://blog.csdn.net/imred/article/details/83759223
set(CMAKE_DEPENDS_IN_PROJECT_ONLY ON)

MESSAGE(STATUS "操作系统类型 ${CMAKE_SYSTEM}")
 
############### 编译器 参数 #####################

set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_STANDARD 17)
if (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
    set (CMAKE_CXX_FLAGS "-std=c++17 ${CMAKE_CXX_FLAGS}")
endif()

# illegal instruction：非法指令
# 考虑是否加了O3优化，对于ARM板来说，O3优化是危险的，使用Os优化吧
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -fPIC -O0 -ggdb3")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -fPIC -O3")

add_compile_options(-fpermissive
                                    -Wno-deprecated-declarations 
                                    -Wno-sign-compare 
                                    -Wno-unused-function 
                                    -Wno-unused-variable
                                    -Wno-reorder)

IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(CMAKE_THREAD_LIBS_INIT "-lpthread")
    set(CMAKE_HAVE_THREADS_LIBRARY 1)
    set(CMAKE_USE_WIN32_THREADS_INIT 0)
    set(CMAKE_USE_PTHREADS_INIT 1)
    set(THREADS_PREFER_PTHREAD_FLAG ON)

    # https://www.iteye.com/blog/chaoslawful-568602
    FIND_PACKAGE(Threads)
    add_compile_options(-pthread) 
    # add_link_options(-static -static-libgcc -static-libstdc++)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
ELSE ()
	MESSAGE(STATUS "其他操作平台: ${CMAKE_SYSTEM_NAME}")
ENDIF ()

# set(BUILD_SHARED_LIBS ON)

############### 依赖库 参数 #####################
# include 设置要放在 ADD_SUBDIRECTORY 之前,
# 否则路径配置或各种变量在子项目不生效.
# Path used for searching by FIND_XXX()
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_INSTALL_PREFIX}/lib/cmake)
MESSAGE(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
MESSAGE(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")

# 部署目录(deploy) 中，头文件所在的相对路径.
set(IncAppend include)

include(GNUInstallDirs)
# 当前项目源码目录, 如此, 
include_directories(${PROJECT_SOURCE_DIR}/${OrgName})
# 当前项目源码目录, 如此, include时不用多带 ${OrgName}
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_INSTALL_PREFIX}/include)
include_directories(${CMAKE_INSTALL_PREFIX})
# OrgName 在根目录的CMakeLists.txt文件中定义.

link_directories(/usr/lib)
link_directories(/usr/local/lib)
link_directories(/usr/local/lib64)
link_directories(/usr/lib/x86_64-linux-gnu)
link_directories(/lib/x86_64-linux-gnu)

# Set RPATH
# Linux和macOS上的默认路径是/usr/local。安装之后执行程序会找不到动态链接库。设置了RPATH之后就不会有问题了。
# 假设安装目录在编译目录的lib子目录内
set(INSTALL_LIB_DIR "../lib:../lib64") 
 
set(CMAKE_SKIP_BUILD_RPATH FALSE)               # 编译时加上RPATH 
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)       # 编译时RPATH不使用安装的RPATH
set(CMAKE_INSTALL_RPATH ${INSTALL_LIB_DIR})     # 安装RPATH为空 
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)     # 安装的执行文件不加上RPATH
 
# 确保链接库不在系统默认安装的目录上时更改到项目lib上
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES ${CMAKE_INSTALL_RPATH} isSystemDir)  
if("${isSystemDir}" STREQUAL "-1")  
  set(CMAKE_INSTALL_RPATH "${INSTALL_LIB_DIR}")
endif("${isSystemDir}" STREQUAL "-1")  
